package com.fy.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fy.core.dto.PageDto;
import com.fy.core.vo.ApiRest;
import com.fy.core.vo.PageResult;
import com.fy.qu.mapper.QuAnswerMapper;
import com.fy.repo.entity.Repo;
import com.fy.sys.entity.Dept;
import com.fy.sys.mapper.DeptMapper;
import com.fy.sys.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author lz
 * @since 2023-06-14
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {





    @Override
    public PageResult<Dept> findPage(PageDto<Dept> pageDto) {

        IPage<Dept> page=new Page<>(pageDto.getNow(), pageDto.getSize());

        Dept where =pageDto.getWhere();
        QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Dept::getDelFlag,"0");

        if (!StringUtils.isEmpty(where.getDeptName())){
            queryWrapper.lambda().like(Dept::getDeptName,where.getDeptName());
        }
        IPage<Dept> pageInfo=this.baseMapper.selectPage(page,queryWrapper);

        PageResult<Dept> pageResult=new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setList(pageInfo.getRecords());

        return pageResult;
    }

    @Override
    public List<Dept> findDepts() {
        return this.baseMapper.findDepts();
    }

    @Override
    public ApiRest add(Dept dept) {
        dept.setParentId(dept.getDeptId());
        dept.setDeptId(null);
        Dept dept1 = this.baseMapper.selectById(dept.getParentId());
        String ancestorsParent = dept1.getAncestors();

        String ancestors = ancestorsParent+","+dept.getParentId();
        dept.setAncestors(ancestors);

        dept.setStatus("0");
        dept.setDelFlag("0");
        dept.setOrderNum(null);
        dept.setCreateTime(new Date());

        this.baseMapper.insert(dept);
        return ApiRest.success("添加成功","");
    }

    @Override
    public ApiRest batchDelete(List<Dept> depts) {
        List<Long> ids = depts.stream().map(item->item.getDeptId()).collect(Collectors.toList());

        for (Long id : ids) {
            this.baseMapper.updateByDelflag(id);
        }

        return ApiRest.success("批量删除题库");
    }

    @Override
    public ApiRest updateByDelflag(Long deptId) {

                this.baseMapper.updateByDelflag(deptId);

        return ApiRest.success("删除成功","");
    }

    @Override
    public ApiRest updateByStatus(Long deptId) {

        Dept dept = this.baseMapper.selectById(deptId);

        UpdateWrapper<Dept> updateWrapper=new UpdateWrapper<>();
        updateWrapper.lambda().eq(Dept::getDeptId,deptId);

        if (dept.getStatus().equals("0")) {
            dept.setStatus("1");
            System.out.println("dept = " + dept);

        }else {
            dept.setStatus("0");
        }
        this.baseMapper.update(dept,updateWrapper);

        return ApiRest.success("修改成功","");
    }
    @Override
    public ApiRest findDepartCascade() {
        return ApiRest.success(this.baseMapper.findDepart());
    }
}
